/**
 * Created by camiez on 2017/3/23.
 */
'use strict';
const express =  require('express');
const ejs = require('ejs');
//创建服务器对象
const app = express();
//创建路由对象
const router = require('./router.js');
//引入body-parser解析form表单提交的信息
const bodyParser = require('body-parser');
//将所有的响应头的conten-type都设为application/json;
app.all('/api/*',(req,res,next)=>{
    //设置允许跨域响应报文头
    //设置跨域
    res.header("Access-Control-Allow-Origin", "*");
    //请求的数据格式
    res.header("Access-Control-Allow-Headers", "X-Requested-With");
    //请求类型
    res.header("Access-Control-Allow-Methods","GET,POST,DELETE,PUT");

    res.setHeader('Content-Type','application/json;charset=utf-8');
    next();
});

//通过ejs渲染模板，配置模板
ejs.delimiter = '$';
app.set('views','./views');
app.set('view engine','ejs');

//处理静态资源文件
app.use('/public',express.static('public'));
//解析body
var urlencodedParser = bodyParser.urlencoded({ extended: false });
//将body-parser固定到应用级中间件
app.use(urlencodedParser)
//把路由绑定应用级中间件上
app.use(router);
//开启服务器
//错误处理中间件
app.use(function(req,res,next,err){
    console.log(err.stack);
})
app.listen(80,function(){
    console.log('开启服务器');
})